package br.com.engine.resources;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

import javax.media.Manager;

import br.com.engine.image.ControleImagem;

import com.thoughtworks.xstream.XStream;

public class ResourceManager 
{
	public static final int IMAGEM        = 1;
	public static final int CONFIGURACOES = 2;
	public static final int XML           = 3;
	public static final int AUDIO         = 4;
	public static final int AUDIOW        = 5;
	public static final int SCENES        = 6;
	
	private static String imgPrefix = "res/imagens/";
	private static String imgSufix  = ".png";
	
	private static String configPrefix = "res/config/";
	private static String configSufix  = "config.properties";
	
	private static String xmlPrefix = "res/mapas/";
	private static String xmlSufix  = ".xml";
	
	private static String audioPrefix = "res/audio/";
	private static String audioSufix  = ".mid";
	
	private static String audioWPrefix = "res/audio/";
	private static String audioWSufix  = ".wav";
	
	private static String scenePrefix = "res/mapas/";
	private static String sceneSufix  = "list_scenes.xml";
	
	@SuppressWarnings("unchecked")
	public static <T> T loadResource( String nome, int nTipo, Class<T> tipo )
	{
		
		switch( nTipo )
		{
			case IMAGEM:
				return (T)carregaImagem( nome );
			case CONFIGURACOES:
				return (T)carregaConfigs( );
			case XML:
				return (T)carregaXML( nome );
			case AUDIO:
			case AUDIOW:
				return (T)carregarAudio( nome, nTipo );
			case SCENES:
				return (T)carregarScenes( );
		}
		
		return null;
	}

	private static Object carregarScenes( )
	{
		Object ret = null;
		
		try 
		{
			XStream xmlParser = new XStream( );
			xmlParser.alias( "mapas", ScenesDefinition.class );
			
			Scanner sc = new Scanner( new File( scenePrefix.concat( sceneSufix ) ) );
			
			String xml = "";
			
			while( sc.hasNextLine( ) )
			{
				xml = xml.concat( sc.nextLine( ) );
			}
			
			ret = xmlParser.fromXML( xml );
			
			sc.close( );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return ret;
	}

	private static Object carregarAudio( String nome, int nTipo )
	{
		Object ret = null;
		
		try 
		{
			String strNomeArq = "";
			
			switch( nTipo )
			{
				case AUDIO:
					strNomeArq = audioPrefix.concat( nome.concat( audioSufix ) );
					break;
				case AUDIOW:
					strNomeArq = audioWPrefix.concat( nome.concat( audioWSufix ) );
					break;
			}
			
			ret = Manager.createRealizedPlayer( new File( strNomeArq ).toURI( ).toURL( ) );
		}
		catch( Exception e )
		{
			e.printStackTrace( );
		}
		
		return ret;
	}

	private static Object carregaXML( String nome )
	{
		try 
		{
			StringBuilder sb = new StringBuilder( );
			
			Scanner sc = new Scanner( new FileInputStream( xmlPrefix.concat( nome.concat( xmlSufix ) ) ) );
			
			while( sc.hasNextLine( ) )
			{
				sb.append( sc.nextLine( ) );
			}
			
			sc.close( );
			
			return sb.toString( );
		} 
		catch( FileNotFoundException e )
		{
			e.printStackTrace( );
		}
		
		return "";
	}

	private static Object carregaConfigs( )
	{
		Properties p = new Properties( );
		
		try 
		{
			p.load( new FileInputStream( configPrefix.concat( configSufix ) ) );
		} 
		catch( IOException e )
		{
			e.printStackTrace( );
		}
		
		return p;
	}

	private static Object carregaImagem( String imageNome )
	{
		return ControleImagem.carregarSprite( imgPrefix + imageNome + imgSufix );
	}
}